1   /*
2    * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package javax.management;
27  
28  import com.sun.jmx.mbeanserver.Introspector;
29  import java.lang.annotation.Annotation;
30  import java.lang.reflect.Method;
31  import java.util.Arrays;
32  
33  /**
34   * Describes a management operation exposed by an MBean.  Instances of
35   * this class are immutable.  Subclasses may be mutable but this is
36   * not recommended.
37   *
38   * @since 1.5
39   */
40  public class MBeanOperationInfo extends MBeanFeatureInfo implements Cloneable {
41  
42      /* Serial version */
43      static final long serialVersionUID = -6178860474881375330L;
44  
45      static final MBeanOperationInfo[] NO_OPERATIONS =
46          new MBeanOperationInfo[0];
47  
48      /**
49       * Indicates that the operation is read-like:
50       * it returns information but does not change any state.
51       */
52      public static final int INFO = 0;
53  
54      /**
55       * Indicates that the operation is write-like: it has an effect but does
56       * not return any information from the MBean.
57       */
58      public static final int ACTION = 1;
59  
60      /**
61       * Indicates that the operation is both read-like and write-like:
62       * it has an effect, and it also returns information from the MBean.
63       */
64      public static final int ACTION_INFO = 2;
65  
66      /**
67       * Indicates that the impact of the operation is unknown or cannot be
68       * expressed using one of the other values.
69       */
70      public static final int UNKNOWN = 3;
71  
72      /**
73       * @serial The method's return value.
74       */
75      private final String type;
76  
77      /**
78       * @serial The signature of the method, that is, the class names
79       * of the arguments.
80       */
81      private final MBeanParameterInfo[] signature;
82  
83      /**
84       * @serial The impact of the method, one of
85       *         <CODE>INFO</CODE>,
86       *         <CODE>ACTION</CODE>,
87       *         <CODE>ACTION_INFO</CODE>,
88       *         <CODE>UNKNOWN</CODE>
89       */
90      private final int impact;
91  
92      /** @see MBeanInfo#arrayGettersSafe */
93      private final transient boolean arrayGettersSafe;
94  
95  
96      /**
97       * Constructs an <CODE>MBeanOperationInfo</CODE> object.  The
98       * {@link Descriptor} of the constructed object will include
99       * fields contributed by any annotations on the {@code Method}
100      * object that contain the {@link DescriptorKey} meta-annotation.
101      *
102      * @param method The <CODE>java.lang.reflect.Method</CODE> object
103      * describing the MBean operation.
104      * @param description A human readable description of the operation.
105      */
106     public MBeanOperationInfo(String description, Method method) {
107         this(method.getName(),
108              description,
109              methodSignature(method),
110              method.getReturnType().getName(),
111              UNKNOWN,
112              Introspector.descriptorForElement(method));
113     }
114 
115     /**
116      * Constructs an <CODE>MBeanOperationInfo</CODE> object.
117      *
118      * @param name The name of the method.
119      * @param description A human readable description of the operation.
120      * @param signature <CODE>MBeanParameterInfo</CODE> objects
121      * describing the parameters(arguments) of the method.  This may be
122      * null with the same effect as a zero-length array.
123      * @param type The type of the method's return value.
124      * @param impact The impact of the method, one of
125      * {@link #INFO}, {@link #ACTION}, {@link #ACTION_INFO},
126      * {@link #UNKNOWN}.
127      */
128     public MBeanOperationInfo(String name,
129                               String description,
130                               MBeanParameterInfo[] signature,
131                               String type,
132                               int impact) {
133         this(name, description, signature, type, impact, (Descriptor) null);
134     }
135 
136     /**
137      * Constructs an <CODE>MBeanOperationInfo</CODE> object.
138      *
139      * @param name The name of the method.
140      * @param description A human readable description of the operation.
141      * @param signature <CODE>MBeanParameterInfo</CODE> objects
142      * describing the parameters(arguments) of the method.  This may be
143      * null with the same effect as a zero-length array.
144      * @param type The type of the method's return value.
145      * @param impact The impact of the method, one of
146      * {@link #INFO}, {@link #ACTION}, {@link #ACTION_INFO},
147      * {@link #UNKNOWN}.
148      * @param descriptor The descriptor for the operation.  This may be null
149      * which is equivalent to an empty descriptor.
150      *
151      * @since 1.6
152      */
153     public MBeanOperationInfo(String name,
154                               String description,
155                               MBeanParameterInfo[] signature,
156                               String type,
157                               int impact,
158                               Descriptor descriptor) {
159 
160         super(name, description, descriptor);
161 
162         if (signature == null || signature.length == 0)
163             signature = MBeanParameterInfo.NO_PARAMS;
164         else
165             signature = signature.clone();
166         this.signature = signature;
167         this.type = type;
168         this.impact = impact;
169         this.arrayGettersSafe =
170             MBeanInfo.arrayGettersSafe(this.getClass(),
171                                        MBeanOperationInfo.class);
172     }
173 
174     /**
175      * <p>Returns a shallow clone of this instance.
176      * The clone is obtained by simply calling <tt>super.clone()</tt>,
177      * thus calling the default native shallow cloning mechanism
178      * implemented by <tt>Object.clone()</tt>.
179      * No deeper cloning of any internal field is made.</p>
180      *
181      * <p>Since this class is immutable, cloning is chiefly of interest
182      * to subclasses.</p>
183      */
184      @Override
185      public Object clone () {
186          try {
187              return super.clone() ;
188          } catch (CloneNotSupportedException e) {
189              // should not happen as this class is cloneable
190              return null;
191          }
192      }
193 
194     /**
195      * Returns the type of the method's return value.
196      *
197      * @return the return type.
198      */
199     public String getReturnType() {
200         return type;
201     }
202 
203     /**
204      * <p>Returns the list of parameters for this operation.  Each
205      * parameter is described by an <CODE>MBeanParameterInfo</CODE>
206      * object.</p>
207      *
208      * <p>The returned array is a shallow copy of the internal array,
209      * which means that it is a copy of the internal array of
210      * references to the <CODE>MBeanParameterInfo</CODE> objects but
211      * that each referenced <CODE>MBeanParameterInfo</CODE> object is
212      * not copied.</p>
213      *
214      * @return  An array of <CODE>MBeanParameterInfo</CODE> objects.
215      */
216     public MBeanParameterInfo[] getSignature() {
217         // If MBeanOperationInfo was created in our implementation,
218         // signature cannot be null - because our constructors replace
219         // null with MBeanParameterInfo.NO_PARAMS;
220         //
221         // However, signature could be null if an  MBeanOperationInfo is
222         // deserialized from a byte array produced by another implementation.
223         // This is not very likely but possible, since the serial form says
224         // nothing against it. (see 6373150)
225         //
226         if (signature == null)
227             // if signature is null simply return an empty array .
228             //
229             return MBeanParameterInfo.NO_PARAMS;
230         else if (signature.length == 0)
231             return signature;
232         else
233             return signature.clone();
234     }
235 
236     private MBeanParameterInfo[] fastGetSignature() {
237         if (arrayGettersSafe) {
238             // if signature is null simply return an empty array .
239             // see getSignature() above.
240             //
241             if (signature == null)
242                 return MBeanParameterInfo.NO_PARAMS;
243             else return signature;
244         } else return getSignature();
245     }
246 
247     /**
248      * Returns the impact of the method, one of
249      * <CODE>INFO</CODE>, <CODE>ACTION</CODE>, <CODE>ACTION_INFO</CODE>, <CODE>UNKNOWN</CODE>.
250      *
251      * @return the impact code.
252      */
253     public int getImpact() {
254         return impact;
255     }
256 
257     @Override
258     public String toString() {
259         String impactString;
260         switch (getImpact()) {
261         case ACTION: impactString = "action"; break;
262         case ACTION_INFO: impactString = "action/info"; break;
263         case INFO: impactString = "info"; break;
264         case UNKNOWN: impactString = "unknown"; break;
265         default: impactString = "(" + getImpact() + ")";
266         }
267         return getClass().getName() + "[" +
268             "description=" + getDescription() + ", " +
269             "name=" + getName() + ", " +
270             "returnType=" + getReturnType() + ", " +
271             "signature=" + Arrays.asList(fastGetSignature()) + ", " +
272             "impact=" + impactString + ", " +
273             "descriptor=" + getDescriptor() +
274             "]";
275     }
276 
277     /**
278      * Compare this MBeanOperationInfo to another.
279      *
280      * @param o the object to compare to.
281      *
282      * @return true if and only if <code>o</code> is an MBeanOperationInfo such
283      * that its {@link #getName()}, {@link #getReturnType()}, {@link
284      * #getDescription()}, {@link #getImpact()}, {@link #getDescriptor()}
285      * and {@link #getSignature()} values are equal (not necessarily identical)
286      * to those of this MBeanConstructorInfo.  Two signature arrays
287      * are equal if their elements are pairwise equal.
288      */
289     @Override
290     public boolean equals(Object o) {
291         if (o == this)
292             return true;
293         if (!(o instanceof MBeanOperationInfo))
294             return false;
295         MBeanOperationInfo p = (MBeanOperationInfo) o;
296         return (p.getName().equals(getName()) &&
297                 p.getReturnType().equals(getReturnType()) &&
298                 p.getDescription().equals(getDescription()) &&
299                 p.getImpact() == getImpact() &&
300                 Arrays.equals(p.fastGetSignature(), fastGetSignature()) &&
301                 p.getDescriptor().equals(getDescriptor()));
302     }
303 
304     /* We do not include everything in the hashcode.  We assume that
305        if two operations are different they'll probably have different
306        names or types.  The penalty we pay when this assumption is
307        wrong should be less than the penalty we would pay if it were
308        right and we needlessly hashed in the description and the
309        parameter array.  */
310     @Override
311     public int hashCode() {
312         return getName().hashCode() ^ getReturnType().hashCode();
313     }
314 
315     private static MBeanParameterInfo[] methodSignature(Method method) {
316         final Class<?>[] classes = method.getParameterTypes();
317         final Annotation[][] annots = method.getParameterAnnotations();
318         return parameters(classes, annots);
319     }
320 
321     static MBeanParameterInfo[] parameters(Class<?>[] classes,
322                                            Annotation[][] annots) {
323         final MBeanParameterInfo[] params =
324             new MBeanParameterInfo[classes.length];
325         assert(classes.length == annots.length);
326 
327         for (int i = 0; i < classes.length; i++) {
328             Descriptor d = Introspector.descriptorForAnnotations(annots[i]);
329             final String pn = "p" + (i + 1);
330             params[i] =
331                 new MBeanParameterInfo(pn, classes[i].getName(), "", d);
332         }
333 
334         return params;
335     }
336 }